package cn.edu.xcu.ckl;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.okgo.OkGo;

import cn.edu.xcu.ckl.api.ApiConstants;
import cn.edu.xcu.ckl.base.BaseActivity;
import cn.edu.xcu.ckl.entity.ProductInfo;
import cn.edu.xcu.ckl.entity.UserInfo;
import cn.edu.xcu.ckl.http.HttpStringCallback;
import cn.edu.xcu.ckl.utils.GlideEngine;


/**
 * 商品详情
 */
public class ProductDetailsActivity extends BaseActivity {
    private ImageView product_img;
    private TextView product_title;
    private TextView product_price;
    private TextView product_details;

    private ProductInfo productInfo;

    private CheckBox shopping_cart;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_product_details;
    }

    @Override
    protected void initView() {
        //初始化控件
        product_img = findViewById(R.id.product_img);
        product_title = findViewById(R.id.product_title);
        product_price = findViewById(R.id.product_price);
        product_details = findViewById(R.id.product_details);
        shopping_cart = findViewById(R.id.shopping_cart);
    }

    @Override
    protected void setListener() {
        //返回
        findViewById(R.id.toolbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //购物车点击事件
        shopping_cart.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setResult(2000);
                finish();
            }
        });

        //加入购物车
        findViewById(R.id.addCar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserInfo userInfo = UserInfo.getUserInfo();
                if (null!=userInfo){
                    new AlertDialog.Builder(mBaseActivity)
                            .setTitle("操作")
                            .setMessage("确定要加入购物车吗？")
                            .setNegativeButton("取消", null)
                            .setPositiveButton("加入购物车", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    addCar(productInfo);
                                }
                            })
                            .show();
                }else{
                    startActivity(new Intent(mBaseActivity, LoginActivity.class));
                    showToast("请先登录");
                }
            }
        });
    }

    @Override
    protected void initData() {
        //获取传递的数据
        productInfo = (ProductInfo) getIntent().getSerializableExtra("productInfo");
        //设置数据
        if (null != productInfo) {
            GlideEngine.createGlideEngine().loadImage(mBaseActivity, productInfo.getProduct_img(), product_img);
            product_title.setText(productInfo.getProduct_title());
            product_details.setText(productInfo.getProduct_details());
            product_price.setText(productInfo.getProduct_price() + "");
        }
    }

    /**
     * 加入购物车
     */
    private void addCar(ProductInfo productInfo) {
        OkGo.<String>get(ApiConstants.ADD_CAR_URL)
                .params("username", UserInfo.getUserInfo().getUsername())
                .params("product_id", productInfo.getProduct_id())
                .params("product_img", productInfo.getProduct_img())
                .params("product_title", productInfo.getProduct_title())
                .params("product_price", productInfo.getProduct_price())
                .params("product_count", 1)
                .execute(new HttpStringCallback(mBaseActivity) {
                    @Override
                    protected void onSuccess(String msg, String response) {
                        showToast(msg);
                        finish();
                    }

                    @Override
                    protected void onError(String response) {

                    }
                });
    }
}